# -*- coding: utf-8 -*-
"""
Created on Wed Nov 15 18:32:55 2017

@author: 51453
"""
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img=cv.imread("D:\wumai9.jpg")
cv.namedWindow("Image",cv.WINDOW_NORMAL)
cv.namedWindow("final",cv.WINDOW_NORMAL)
cv.namedWindow("HISTO",cv.WINDOW_NORMAL)
cv.namedWindow("histoandlap",cv.WINDOW_NORMAL)
im2=np.array(img)
cv.imshow("Image",img)
a=img.shape[0]
b=img.shape[1]
c=img.shape[2]
chans = cv.split(img)
colors = ("b","g","r")
plt.figure()
plt.title("Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
for (chan,color) in zip(chans,colors):
    hist = cv.calcHist([chan],[0],None,[256],[0,256])
    plt.plot(hist,color = color)
    plt.xlim([0,256])
plt.show()#用于显示直方图
hist,bins = np.histogram(img.flatten(),256,[0,256])   
cdf = hist.cumsum() #计算累积直方图
cdf_m = np.ma.masked_equal(cdf,0) #除去直方图中的0值   
cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
cdf = np.ma.filled(cdf_m,0).astype('uint8') #将掩模处理掉的元素补为0  
result = cv.LUT(img, cdf)
chans = cv.split(result)
colors = ("b","g","r")
plt.figure()
plt.title("Flattened Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
for (chan,color) in zip(chans,colors):
    hist = cv.calcHist([chan],[0],None,[256],[0,256])
    plt.plot(hist,color = color)
    plt.xlim([0,256]) 
im4=np.array(result)##im4=im2+lap
im2=np.array(result)##im2=result=histogram
im3=np.array(result)##im3=gaussian
im5=np.array(result)##im5=histo+lap and unsharpmasking

for i in range(1,a-1):
    for j in range(1,b-1):
        if im2[i][j][0]+im2[i][j][1]+im2[i][j][2]<30:
            im4[i][j][0]=im2[i][j][0]
            im4[i][j][1]=im2[i][j][1]
            im4[i][j][2]=im2[i][j][2]
            break
        im4[i][j][0]=0
        im4[i][j][1]=0
        im4[i][j][2]=0
        im4[i][j][0]=(10*im2[i][j][0]-im2[i-1][j][0]-im2[i+1][j][0]-im2[i][j-1][0]-im2[i][j+1][0]-im2[i+1][j+1][0]-im2[i+1][j-1][0]-im2[i-1][j+1][0]-im2[i-1][j-1][0])/2
        im4[i][j][1]=(10*im2[i][j][1]-im2[i-1][j][1]-im2[i+1][j][1]-im2[i][j-1][1]-im2[i][j+1][1]-im2[i+1][j+1][1]-im2[i+1][j-1][1]-im2[i-1][j+1][1]-im2[i-1][j-1][1])/2
        im4[i][j][2]=(10*im2[i][j][2]-im2[i-1][j][2]-im2[i+1][j][2]-im2[i][j-1][2]-im2[i][j+1][2]-im2[i+1][j+1][2]-im2[i+1][j-1][2]-im2[i-1][j+1][2]-im2[i-1][j-1][2])/2
        if im4[i][j][0]<0:
            im4[i][j][0]=0
        if im4[i][j][1]<0:
            im4[i][j][1]=0
        if im4[i][j][2]<0:
            im4[i][j][2]=0
        if (im4[i-1][j][0]+im4[i+1][j][1])-2*im4[i][j][2]>150:
            im4[i][j][2]=(im4[i-1][j][2]+im4[i+1][j][2])/2
            break
        if (im4[i-1][j][0]+im4[i+1][j][2])-2*im4[i][j][1]>150:
            im4[i][j][1]=(im4[i-1][j][1]+im4[i+1][j][1])/2
            break
        if (im4[i-1][j][2]+im4[i+1][j][1])-2*im4[i][j][0]>150:
            im4[i][j][0]=(im4[i-1][j][0]+im4[i+1][j][0])/2
            break
for i in range(1,a-1):
    for j in range(1,b-1):
        im3[i][j][0]=(4*result[i][j][0]+2*result[i-1][j][0]+2*result[i+1][j][0]+2*result[i][j-1][0]+2*result[i][j+1][0]+result[i+1][j-1][0]+result[i+1][j+1][0]+result[i-1][j+1][0]+result[i-1][j-1][0])/16
        im3[i][j][1]=(4*result[i][j][1]+2*result[i-1][j][1]+2*result[i+1][j][1]+2*result[i][j-1][1]+2*result[i][j+1][1]+result[i+1][j-1][1]+result[i+1][j+1][1]+result[i-1][j+1][1]+result[i-1][j-1][1])/16
        im3[i][j][2]=(4*result[i][j][2]+2*result[i-1][j][2]+2*result[i+1][j][2]+2*result[i][j-1][2]+2*result[i][j+1][2]+result[i+1][j-1][2]+result[i+1][j+1][2]+result[i-1][j+1][2]+result[i-1][j-1][2])/16
for i in range(1,a-1):
    for j in range(1,b-1):
        
        if im4[i][j][0]>im3[i][j][0]:
            im5[i][j][0]=im4[i][j][0]+(im4[i][j][0]-im3[i][j][0])*0.5
        else:
            im5[i][j][0]=im4[i][j][0]-(im3[i][j][0]-im4[i][j][0])*0.5
        if im4[i][j][1]>im3[i][j][1]:
            im5[i][j][1]=im4[i][j][1]+(im4[i][j][1]-im3[i][j][1])*0.5
        else:
            im5[i][j][1]=im4[i][j][1]-(im3[i][j][1]-im4[i][j][1])*0.5
        if im4[i][j][2]>im3[i][j][2]:
            im5[i][j][2]=im4[i][j][2]+(im4[i][j][2]-im3[i][j][2])*0.5
        else:
            im5[i][j][2]=im4[i][j][2]-(im3[i][j][2]-im4[i][j][2])*0.5
        #print('r',im5[i][j][0],'g',im5[i][j][1],'b',im5[i][j][2])       
cv.imshow('HISTO',result)
cv.imshow('histoandlap',im4)       
cv.imshow('final',im5)
cv.waitKey(0)